<?php
namespace app;

use think\facade\Request;
use think\facade\Db;
use think\response\Json;

class H5AuthController extends BaseController
{
    /**
     * @var array|int
     */

    /**
     * AppAuthController constructor.
     */
    public function __construct()
    {
        if(!$this->token()){ exit(json_encode(['code' => 301, 'msg' => 'token无效或过期'])); }
    }

    public function token()
    {
        $token = Request::header('token');
        if($token){
            $user = $this->check_login($token);
            
            if($user){
                $this->user = $user;
                return true;
            }
        }
        return false;
    }

    public function check_login($token)
    {
        if (!$token) return 0;


//        $token = explaintoken($token);
//        $uid = $token->id;
//
//        print_r($uid);
//
//        exit();

        try {
             $token = explaintoken($token);
             $uid = $token->id;

//             exit($uid);
//            $uid = 1;

            return Db::name('user_doc')->where('id', $uid)->find();
        } catch (Exception $e) {
            return [];
        }
    }
}
